package com.mailbox.mailbox.controller;

import com.mailbox.mailbox.domain.SecurityUser;

import com.mailbox.mailbox.service.UserService;
import com.mailbox.mailbox.service.VisitorService;
import com.mailbox.mailbox.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
/**
 *
 * @author
 */
@Api(tags = "用户接口")
@Slf4j
public class UserController {
    @Resource
    UserService userService;
    @Resource
    VisitorService visitorService;

    @ApiOperation(value = "添加管理员(用户),前端不交互")
    @PostMapping("/Admin/addAdmin")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "username", value = "账号",required = true),
                    @ApiImplicitParam(name = "password", value = "密码",required = true),
                    @ApiImplicitParam(name = "chekPassword", value = "确认密码",required = true),
                    @ApiImplicitParam(name = "nickname", value = "昵称",required = true)
            }
    )
    public ResponseResult addAdmin(String username,String password,String chekPassword,String nickname){
        //做简单空值判断
        if(StringUtils.isAnyBlank(username,nickname)){
            return ResponseResult.error("账号或昵称为空");
        }
        if(StringUtils.isAnyBlank(password)){
            return ResponseResult.error("密码为空");
        }
        if(!StringUtils.equals(password,chekPassword)){
            return ResponseResult.error("两次密码不一致");
        }
       return userService.addAdmin(username, password, chekPassword, nickname);
    }



    @ApiOperation(value = "游客注册")
    @PostMapping("/User/register")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "phoneNumber", value = "电话",required = true),
                    @ApiImplicitParam(name = "password", value = "密码",required = true),
                    @ApiImplicitParam(name = "chekPassword", value = "确认密码",required = true),
                    @ApiImplicitParam(name = "nickname", value = "真实姓名",required = true),
                    @ApiImplicitParam(name = "email", value = "邮箱",required = true),
                    @ApiImplicitParam(name = "username", value = "学号/工号",required = true)
            }
    )
    public ResponseResult register(String phoneNumber,String password,String chekPassword,String nickname,String email,String username){
        //做简单空值判断
        if(StringUtils.isAnyBlank(phoneNumber)){
            return ResponseResult.error("电话号码为空");
        }
        if(StringUtils.isAnyBlank(password)){
            return ResponseResult.error("密码为空");
        }
        if(StringUtils.isAnyBlank(chekPassword)){
            return ResponseResult.error("确认密码为空");
        }
        if(StringUtils.isAnyBlank(nickname)){
            return ResponseResult.error("名字为空");
        }
        if(StringUtils.isAnyBlank(email)){
            return ResponseResult.error("邮箱为空");
        }
        if(!StringUtils.equals(password,chekPassword)){
            return ResponseResult.error("两次密码不一致");
        }
        if(StringUtils.isAnyBlank(username)){
            return ResponseResult.error("学号/工号不能为空");
        }
    return userService.register(phoneNumber, password, chekPassword, nickname, email,username);
    }

    @ApiOperation(value = "管理员(用户登录)")
    @PostMapping("/Admin/adminLogin")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "username", value = "账号:学号/工号",required = true),
                    @ApiImplicitParam(name = "password", value = "密码",required = true)
            }
    )
    public ResponseResult adminLogin(String username,String password){
        return userService.adminLogin(username,password);
    }

    @ApiOperation(value = "修改账号密码(修改完后退出登录)")
    @PutMapping("/User/updateUserPassword")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "oldPassword", value = "旧密码",required = true),
                    @ApiImplicitParam(name = "newPassword", value = "新密码",required = true)
            }
    )
    public ResponseResult updateUserPassword(String oldPassword,String newPassword,@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser){
        Integer userId=securityUser.getUserId();
        if (StringUtils.isAnyBlank(oldPassword)) {
            return ResponseResult.error("旧密码为空,请重新填写");
        }
        if(StringUtils.isAnyBlank(newPassword)){
            return ResponseResult.error("新密码为空,请重新填写");
        }
        if(userId==null){
            return ResponseResult.error("id为空");
        }
        return userService.updateUserPassword(oldPassword, newPassword, userId);
    }

    @ApiOperation(value = "找回密码-->通过密令找回密码")
    @PostMapping("/Admin/findPassword")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "authToken", value = "密令",required = true),
                    @ApiImplicitParam(name = "newPassword", value = "新密码",required = true),
                    @ApiImplicitParam(name = "username", value = "账号",required = true)
            }
    )
    public ResponseResult findPassword(String authToken,String username,String newPassword){
        if(StringUtils.isAnyBlank(authToken)){
            return ResponseResult.error("密令为空,请重新填写");
        }
        if(StringUtils.isAnyBlank(username)){
            return ResponseResult.error("账号为空,请重新填写");
        }
        if(StringUtils.isAnyBlank(newPassword)){
            return ResponseResult.error("新密码为空,请重新填写");
        }
        return userService.findPassword(authToken,username,newPassword);
    }
    @ApiOperation(value = "修改用户昵称")
    @PutMapping("/User/updateNickname")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "newNickname", value = "新昵称",required = true)
            }
    )
    public ResponseResult updateNickname(String newNickname,@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser){
        Integer userId=securityUser.getUserId();
        if(StringUtils.isAnyBlank(newNickname)){
            return ResponseResult.error("新昵称为空,请重新填写");
        }
        if(userId==null){
            return ResponseResult.error("用户id为空");
        }
        return userService.updateNickname(newNickname,userId);
    }


    @ApiOperation(value = "遍历用户信息")
    @GetMapping("/Visitor/updateNickname")
    public ResponseResult UserInfo(@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser){
        Integer userId=securityUser.getUserId();
        if(userId==null){
            return ResponseResult.error("用户id为空");
        }
        return userService.UserInfo(userId);
    }


//    @ApiOperation(value = "退出登录")
//    @PostMapping("/User/logout")
//    public ResponseResult logout(@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser){
//        Integer userId=securityUser.getUserId();
//        if(userId==null){
//            return ResponseResult.error("用户id为空");
//        }
//        return userService.logout(userId);
//    }

//    @ApiOperation("用户登录")
//    @PostMapping("/User/login")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "username", paramType = "query", value = "账号",dataTypeClass = String.class),
//            @ApiImplicitParam(name = "password", paramType = "query", value = "密码",dataTypeClass = String.class)
//    })
//    public void  login(String username,String password){}

}
